package com.Spring.test_2_3_spring_principle.component;

import com.Spring.test_2_3_spring_principle.model.User;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Component
public class BeanLifeComponent implements BeanNameAware {
    // Bean 的生命周期
    private User user;

    public BeanLifeComponent() {
        System.out.println("执行构造方法");
    }
    @Qualifier("user1")// 注入的是哪一个
    @Autowired
    public void setUser1(User user) {
        this.user = user;
        System.out.println("属性注入");
    }
    @PostConstruct // 这个注解实现初始化
    public void init(){
        System.out.println("执行初始化方法");
    }

    public void use(){
        System.out.println("执行use方法");
    }

    @PreDestroy // 销毁前执行
    public void destroy(){
        //bean 销毁前执行的
        System.out.println("执行destroy方法");
    }

    @Override
    public void setBeanName(String s) {
        System.out.println("执行beanNameAware,s:"+s);
    }
}
